import request from '/@/utils/request';
import { Session } from '/@/utils/storage';

import qs from 'qs'

export function useSysApi() {
    return {
        /**
         * 操作权限列表
         * @param md5Str 
         * @param page 
         * @param pageSize 
         * @param queryParams 
         * @param microserviceName 
         * @returns 
         */
        queryOperateLegalPower: (md5Str: any, page: any, pageSize: any, queryParams: any, microserviceName: any = "") => {
            if (!microserviceName) {
                microserviceName = 'COMMON-SERVICE';
            }
            return request({
                url: `${microserviceName}/common/dynamic/sql/paged/${md5Str}/${page}/${pageSize}`,
                method: "post",
                data: queryParams
            });
        },

        /**
         *  查询消息日志
         * @param data 
         * @returns 
         */
        queryMessageLog: (data: object) => {
            let objStr = qs.stringify(data);
            return request({
                url: `COMMON-SERVICE/admin/message/logs?${objStr}`,
                method: "get"
            });
        },


        /**
         * 获取微服务列表
         * @returns 
         */
        getServices: () => {
            return request({
                url: `COMMON-SERVICE/discovery/client/getServices`,
                method: "get"
            });
        },

        /**
         * 同步微服务
         * @returns 
         */
        registerResource: (microName: string) => {
            return request({
                url: `${microName}/system/resource/register/all`,
                method: "post"
            });
        },

        /**
         * 根据服务ID获取服务详情
         * @param serviceId 
         * @returns 
         */
        getServiceInstances: (serviceId: any) => {
            return request({
                url: `COMMON-SERVICE/discovery/client/getServiceInstances?serviceId=${serviceId}`,
                method: "get"
            });
        },


        /**
         * 获取字典code
         * @param dicCode 
         * @param belongMicroService 
         * @param language 
         */
        selectDicItemsByDicCode: (dicCode: any, belongMicroService: any = "General", language: any = 'zh_CN') => {
            let params = { "dicCode": dicCode, "microServiceName": belongMicroService, "language": language }
            return request({
                url: 'COMMON-SERVICE/admin/dic/selectDicItemsByDicCodeFromCache',
                method: 'get',
                params: params
            })
        },

        selectDicItemsByDicCodeToNameAndValue: (dicCode: String, belongMicroService: String, language: String, dataType: String) => {
            return new Promise(function (resolve) {

                let params = { "dicCode": dicCode, "microServiceName": "General", "language": "zh_CN", "dataType": "String" }
                if (language) {
                    params = Object.assign(params, { "language": language });
                }
                if (belongMicroService) {
                    params = Object.assign(params, { "microServiceName": belongMicroService });
                }
                if (dataType) {
                    params = Object.assign(params, { "dataType": dataType });
                }
                let key = "systemdic." + params.dicCode + "." + params.microServiceName + "." + params.language + "." + params.dataType;
                if (sessionStorage.getItem(key)) {
                    resolve(JSON.parse(sessionStorage.getItem(key)));
                    return;
                }
                let datas: any = []
                useSysApi().selectDicItemsByDicCode(dicCode, belongMicroService, language).then(data => {
                    data.forEach((item: { dicItemValue: string; dicItemName: any; dicItemCode: any; extAttribut1: any; extAttribut2: any; extAttribut3: any; extAttribut4: any; extAttribut5: any; dicItemId: any; dicMasterId: any; }) => {
                        let value: any = item.dicItemValue;
                        if (!dataType) {
                            value = item.dicItemValue
                        } else if ("Number" == dataType) {
                            value = Number(item.dicItemValue);
                        } else if ("Int" == dataType) {
                            value = Number.parseInt(item.dicItemValue);
                        } else if ("Float" == dataType) {
                            value = Number.parseFloat(item.dicItemValue);
                        }

                        let valueFinal = {
                            value: value,
                            label: item.dicItemName,
                            text: item.dicItemName,
                            code: item.dicItemCode,
                            extAttribut1: item.extAttribut1,
                            extAttribut2: item.extAttribut2,
                            extAttribut3: item.extAttribut3,
                            extAttribut4: item.extAttribut4,
                            extAttribut5: item.extAttribut5,
                            dicItemId: item.dicItemId,
                            dicMasterId: item.dicMasterId
                        };

                        datas.push(valueFinal);

                    });
                    sessionStorage.setItem(key, JSON.stringify(datas));
                    resolve(datas)
                })
            });

        },


        /**
         * 根据分页获取数据字典数据
         * @param page 
         * @param pageSize 
         * @param params 
         * @returns 
         */
        queryPagedSystemDic: (page: any, pageSize: any, params: any) => {
            return request({
                url: `COMMON-SERVICE/admin/dic/page/list/${page}/${pageSize}`,
                method: 'post',
                data: params
            })
        },



        /**
         * 数据字典的保存或修改
         * @param records 
         * @returns 
         */
        dicSaveOrUpdate: (records: any) => {
            return request({
                url: `COMMON-SERVICE/admin/dic/saveOrUpdate`,
                method: 'post',
                data: records
            })
        },

        /**
         * 数据字典的刷新缓存
         * @returns 
         */
        dicRefreshCache: () => {
            return request({
                url: `COMMON-SERVICE/admin/dic/refreshCache`,
                method: 'get'
            })
        },


        /**
         * 根据ID查询数据字典信息
         * @param dicMasterId 
         * @returns 
         */
        queryDicItemsById: (dicMasterId: any) => {
            return request({
                url: `COMMON-SERVICE/admin/dic/selectDicItemsByDicMasterId/${dicMasterId}`,
                method: 'get'
            })
        },


        /**
         * 获取所有的定时任务
         * @param params 
         * @returns 
         */
        getAllJobs: (params: any) => {
            return request({
                url: `COMMON-SERVICE/quartz/client/getAll`,
                method: 'get',
                params: params
            })
        },

        /**
         * 暂停任务
         * @param microName 
         * @param params 
         * @returns 
         */
        quartzPause: (microName: any, params: any) => {
            return request({
                url: `${microName}/quartz/job/pause`,
                method: 'post',
                data: params
            })
        },

        /**
         * 重启定时任务
         * @param microName 
         * @param params 
         * @returns 
         */
        quartzResume: (microName: any, params: any) => {
            return request({
                url: `${microName}/quartz/job/resume`,
                method: 'post',
                data: params
            })
        },
        /**
         * 执行定时任务
         * @param microName 
         * @param params 
         * @returns 
         */
        quartzRunNow: (microName: any, params: any) => {
            return request({
                url: `${microName}/quartz/job/runNow`,
                method: 'post',
                data: params
            })
        },
        /**
         * 定时任务的保存与更新
         * @param microName 
         * @param params 
         * @returns 
         */
        quartzCreateOrupdate: (microName: any, params: any) => {
            return request({
                url: `${microName}/quartz/job/createOrUpdate`,
                method: 'post',
                data: params
            })
        },
        /**
         * 动态SQL的查询
         * @param md5Str 
         * @param page 
         * @param pageSize 
         * @param queryParams 
         * @param microserviceName 
         */
        queryDynamicSqlPaged: (md5Str: any, page: any, pageSize: any, queryParams: any, microserviceName: string = "") => {
            if (!microserviceName) {
                microserviceName = 'COMMON-SERVICE';
            }
            return request({
                url: `${microserviceName}/common/dynamic/sql/paged/${md5Str}/${page}/${pageSize}`,
                method: 'post',
                data: queryParams
            })
        },


        /**
         * 获取接口配置信息
         * @param pageNum 
         * @param pageSize 
         * @param pagedQueryParms 
         * @returns 
         */
        pagedQueryDataInterface: (pageNum: any, pageSize: any, pagedQueryParms: any) => {
            return request({
                url: `COMMON-SERVICE/admin/mongo/config/dataInterface/list/${pageNum}/${pageSize}`,
                method: 'post',
                data: pagedQueryParms
            })
        },


        /**
         * 数据接口的状态改失效
         * @param ObjectIds 
         * @returns 
         */
        dataInterfaceInactiveByIds: (ObjectIds: any) => {
            return request({
                url: `COMMON-SERVICE/admin/mongo/config/dataInterface/inactiveByIds`,
                method: 'post',
                data: ObjectIds
            })
        },

        /**
        * 数据接口的状态改有效
        * @param ObjectIds 
        * @returns 
        */
        dataInterfaceActiveByIds: (ObjectIds: any) => {
            return request({
                url: `COMMON-SERVICE/admin/mongo/config/dataInterface/activeByIds`,
                method: 'post',
                data: ObjectIds
            })
        },


        /**
         * 数据接口的保存修改
         * @param dataInterfaceList 
         * @returns 
         */
        dataInterfaceSaveOrUpdate: (dataInterfaceList: any) => {
            return request({
                url: `COMMON-SERVICE/admin/mongo/config/dataInterface/saveOrUpdate`,
                method: 'post',
                data: dataInterfaceList
            })
        },

        /**
         * 数据接口的删除（批量）
         * @param ObjectIds 
         * @returns 
         */
        dataInterfaceDeleteByIds: (ObjectIds: any) => {
            return request({
                url: `COMMON-SERVICE/admin/mongo/config/dataInterface/deleteByIds`,
                method: 'post',
                data: ObjectIds
            })
        },

        /**
         * 查看数据
         * @param userId 
         * @returns 
         */
        queryUserDataDimensionListByUserId: (userId: any) => {
            return request({
                url: `COMMON-SERVICE/admin/dataDimension/selectDataDimensionByUserId?userId=${userId}`,
                method: 'get'
            })
        },



        /**
         * 保存或修改系统用户
         * @param params 
         * @returns 
         */
        saveOrUpdateFrameUser: (params: any) => {
            return request({
                url: `COMMON-SERVICE/admin/user/saveOrUpdate`,
                method: 'post',
                data: params
            })
        },


        /**
         * sql获取数据列表
         * @param md5Str 
         * @param queryParams 
         * @param microserviceName 
         * @returns 
         */
        queryDynamicSqlList: (md5Str: any, queryParams: any, microserviceName: any = 'COMMON-SERVICE') => {
            return request({
                url: `${microserviceName}/common/dynamic/sql/list/${md5Str}`,
                method: 'post',
                data: queryParams
            })
        },


        /**
         * 禁用用户
         * @param userId 
         * @returns 
         */
        disabledUserFrame: (userId: any) => {
            return request({
                url: `COMMON-SERVICE/userinfo/disabledUser/${userId}`,
                method: 'get'
            })
        },

        /**
         * 启动用户
         * @param userId 
         * @returns 
         */
        enabledUserFrame: (userId: any) => {
            return request({
                url: `COMMON-SERVICE/userinfo/enabledUser/${userId}`,
                method: 'get'
            })
        },


        /**
         * 重置密码
         * @param userId 
         * @returns 
         */
        resetPasswordFrameUser: (userId: any) => {
            return request({
                url: `COMMON-SERVICE/admin/user/resetPassword/${userId}`,
                method: 'get'
            })
        },

        /**
         * 重置谷歌
         * @param userId 
         * @returns 
         */
        resetGoogleBindFlag: (userId: any) => {
            return request({
                url: `COMMON-SERVICE/user/mfa/resetBindUserGoogleSecretFlag/${userId}`,
                method: 'get'
            })
        },

        /**
         * 获取用户的数据权限
         * @param userId 
         * @returns 
         */
        queryUserDataDimensionList: (userId: any, dataDimensionType: any = 20) => {
            return request({
                url: `COMMON-SERVICE/admin/dataDimension/queryDataDimensionListByTypeAndId?dataDimensionType=${dataDimensionType}&referId=${userId}`,
                method: 'get'
            })
        },


        /**
         * 保存用户的数据权限
         * @param dataDimensionList 
         * @returns 
         */
        userDataDimensionInsertOrUpdateBatch: (dataDimensionList: any) => {
            return request({
                url: `COMMON-SERVICE/admin/dataDimension/insertOrUpdateBatch`,
                method: 'post',
                data: dataDimensionList
            })
        },


        /**
         * 保存或修改角色信息
         * @param params 
         * @returns 
         */
        saveOrUpdateFrameRole: (params: any) => {
            return request({
                url: `COMMON-SERVICE/admin/role/saveOrUpdate`,
                method: 'post',
                data: params
            })
        },



        /**
         * 获取获取操作日志
         * dendy 2023年2月18日 23:36:04
         * @param pageNum 
         * @param pageSize 
         * @param pagedQueryParms 
         * @returns 
         */
        pagedGetOperateLog: (pageNum: any, pageSize: any, pagedQueryParms: any) => {
            return request({
                url: `COMMON-SERVICE/admin/mongo/OperateLog/list/${pageNum}/${pageSize}`,
                method: 'post',
                data: pagedQueryParms
            })
        },

        /**
         *  查询定时任务日志
         * @param data 
         * @returns 
         */
        queryJobLog: (data: object) => {
            let objStr = qs.stringify(data);
            return request({
                url: `COMMON-SERVICE/admin/job/logs?${objStr}`,
                method: "get"
            });
        },


        /**
         * 获取获取操作日志
         * dendy 2023年2月18日 23:36:04
         * @param pageNum 
         * @param pageSize 
         * @param pagedQueryParms 
         * @returns 
         */
        checkUserOperator: (operateAuthList: String[]) => {
            return request({
                url: `COMMON-SERVICE/userinfo/checkCurrentUserOperator`,
                method: 'post',
                data: operateAuthList
            })
        },


        checkCurrentUserOperator: (serviceName: String, path: String, operCode: String) => {
            return new Promise(function (resolve) {
                let returnVal = false;
                const key = serviceName + "::" + path + "::" + operCode;

                if (Session.get(key) != null) {
                    returnVal = Session.get(key);
                } else {
                    useSysApi().checkUserOperator([key]).then(data => {
                        returnVal = data.checkResult[key];
                        Session.set(key, returnVal);
                    });
                }
                resolve(returnVal);
            });

        },
    };
}
